﻿using AutoMapper;
using Enferno.Public.InversionOfControl;
using Enferno.Public.Web.Mappers.Resolvers;
using Enferno.Public.Web.Models;
using Enferno.StormApiClient.Products;

namespace Enferno.Public.Web.Mappers.ProductProfiles
{
    public class ProductItemToProductItemModelProfile :
        ProductItemToProductItemBaseModelProfile
            <ProductItemModel, VariantModel, ProductFileModel, ProductManufacturerModel, PriceModel, OnHandStatusModel,
                ParametricModel, ParametricValueModel>
    {
        
    }

    public class ProductItemToCustomProductItemModelProfile<TProductItem> :
        ProductItemToProductItemBaseModelProfile
            <TProductItem, VariantModel, ProductFileModel, ProductManufacturerModel, PriceModel, OnHandStatusModel,
                ParametricModel, ParametricValueModel>
        where TProductItem : ProductItemCustomVariantModel<VariantModel>
    {
        
    }  

    public class ProductItemToProductItemBaseModelProfile<
        TProductItemModel, 
        TVariantModel, 
        TProductFileModel, 
        TProductManufacturerModel, 
        TPriceModel, 
        TOnHandStatusModel, 
        TParametricModel, 
        TParametricValueModel> : Profile
        where TProductItemModel: ProductItemBaseModel<TVariantModel, TProductFileModel, TProductManufacturerModel, TPriceModel, TOnHandStatusModel, TParametricModel, TParametricValueModel>
        where TVariantModel: VariantBaseModel<TOnHandStatusModel, TPriceModel, TProductFileModel, TParametricModel, TParametricValueModel>
        where TProductFileModel: ProductFileModel, new()
        where TProductManufacturerModel: ProductManufacturerModel
        where TPriceModel: PriceModel
        where TOnHandStatusModel: OnHandStatusModel, new()
        where TParametricModel: ParametricBaseModel<TParametricValueModel>
        where TParametricValueModel: ParametricValueModel
    {
        protected override void Configure()
        {
            var siteRules = IoC.Resolve<ISiteRules>();

            Mapper.CreateMap<ProductItem, TProductItemModel>()
                .ForMember(to => to.Url, opts => opts.ResolveUsing(siteRules.GetProductPageUrl))
                .ForMember(to => to.Price, opts => opts.Ignore()) //is mapped in the builder
                .ForMember(to => to.Flags, opts => opts.ResolveUsing<ProductItemFlagsResolver>())
                .ForMember(to => to.Files, opts => opts.ResolveUsing<ProductItemFilesResolver<TProductFileModel>>())
                .ForMember(to => to.OnHandStatus, opts => opts.ResolveUsing<ProductItemOnHandStatusResolver<TOnHandStatusModel>>())
                .ForMember(to => to.Variants, opts => opts.Ignore()) //mapped in the builder
                .ForMember(to => to.Parametrics, opts => opts.Ignore());
        }
    }
}
